//
//  MeSettingTableViewController.m
//  糗事百科
//
//  Created by 奚超明 on 16/6/4.
//  Copyright © 2016年 奚超明. All rights reserved.
//

#import "MeSettingTableViewController.h"
#import "LoginViewController.h"
#import "AccountAndSafeTableViewController.h"
#import "NormalSettingTableViewController.h"
#import "MessageSettingTableViewController.h"

@interface MeSettingTableViewController ()
/** 设置模块数组 */
@property (nonatomic, strong) NSMutableDictionary *settingDic;

@end

@implementation MeSettingTableViewController

static NSString *settingID = @"mySetting";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStyleGrouped];
    
    // 标题
    self.navigationItem.title = @"设置";
    
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // 验证是否登录
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"YES"]) { // 登录
        return self.settingDic.count;
    } else {
        return self.settingDic.count - 1;
    }
    
}

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *valueArr = (NSArray *)[self.settingDic objectForKey:[self ascendingKeyArray][section]];
    return valueArr.count;
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingID];
    if (cell == nil) {
        if (indexPath.section == 4) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settingID];
        } else {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:settingID];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSArray *valueArr = (NSArray *)[self.settingDic objectForKey:[self ascendingKeyArray][indexPath.section]];
    
    cell.textLabel.text = valueArr[indexPath.row];
    
    cell.textLabel.textColor = kRGBColor(118, 118, 136);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 4) { // 退出登录cell
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
    } else if (indexPath.section == 3 && indexPath.row == 0) { // 清除缓存cell
        // 获得缓存
        CGFloat size = [SDImageCache sharedImageCache].getSize / 1000.0 / 1000;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fMB", size];
    }
    
    return cell;
}

#pragma mark - 获得一个有序key数组
- (NSArray *)ascendingKeyArray
{
    NSArray *keyArr = [[self.settingDic allKeys] mutableCopy];
    NSArray *newKeyArr = [keyArr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if([obj1 integerValue] < [obj2 integerValue]){
            return NSOrderedAscending;
        }
        if([obj1 integerValue] > [obj2 integerValue]){
            return NSOrderedDescending;
        }
        return NSOrderedSame;
    }];
    return newKeyArr;
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) { // 帐号与安全
        [self userNameAndSafe];
    } else if (indexPath.section == 1 && indexPath.row == 0) { // 消息设置
        [self messageSetting];
    } else if (indexPath.section == 1 && indexPath.row == 1) { // 小纸条
        [self note];
    } else if (indexPath.section == 2 && indexPath.row == 0) { // 黑名单
        [self blacklist];
    } else if (indexPath.section == 2 && indexPath.row == 1) { // 常规
        [self generalSetting];
    } else if (indexPath.section == 3 && indexPath.row == 0) { // 清除缓存
        [self clearCache];
    } else if (indexPath.section == 3 && indexPath.row == 1) { // 意见反馈
        [self leaveMessage];
    } else if (indexPath.section == 3 && indexPath.row == 2) { // 关于糗百
        [self aboutQiuBai];
    } else { // 退出登录
        [self logout];
    }
}

#pragma mark - 帐号与安全
- (void)userNameAndSafe
{
    AccountAndSafeTableViewController *accountAndSafeVC = [[AccountAndSafeTableViewController alloc] init];
    [self.navigationController pushViewController:accountAndSafeVC animated:YES];
}

#pragma mark - 消息设置
- (void)messageSetting
{
    MessageSettingTableViewController *normalSettingVC = [[MessageSettingTableViewController alloc] init];
    [self.navigationController pushViewController:normalSettingVC animated:YES];
}

#pragma mark - 小纸条
- (void)note
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"亲~功能暂未实现" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}

#pragma mark - 黑名单
- (void)blacklist
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"亲~功能暂未实现" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}

#pragma mark - 常规
- (void)generalSetting
{
    NormalSettingTableViewController *normalSettingVC = [[NormalSettingTableViewController alloc] init];
    [self.navigationController pushViewController:normalSettingVC animated:YES];
}

#pragma mark - 清除缓存
- (void)clearCache
{
    [[SDImageCache sharedImageCache] clearDisk];
    
    // 获得缓存
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    CGFloat size = [SDImageCache sharedImageCache].getSize / 1000.0 / 1000;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fMB", size];
    
    // 提示是否退出登录提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"清除缓存成功" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}

#pragma mark - 意见反馈
- (void)leaveMessage
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"亲~功能暂未实现" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}

#pragma mark - 关于糗百
- (void)aboutQiuBai
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"亲~功能暂未实现" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}

#pragma mark - 退出登录 或 登录/注册
- (void)logout
{
    // 提示是否退出登录提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *enterAction = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        // 注销登录
        NSInteger platformType = [[[NSUserDefaults standardUserDefaults] objectForKey:@"platformType"] integerValue];
        [ShareSDK cancelAuthorize:platformType];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"logout" object:nil];
        
        // 跳转界面
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alert addAction:enterAction];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:cancelAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - 懒加载
- (NSMutableDictionary *)settingDic
{
    if (!_settingDic) {
        _settingDic = [@{
                         @"0" : @[@"帐号与安全"],
                         @"1" : @[@"消息设置", @"小纸条"],
                         @"2" : @[@"黑名单", @"常规"],
                         @"3" : @[@"清除缓存", @"意见反馈", @"关于糗百"],
                         @"4" : @[@"退出登录"]
                         } mutableCopy];
    }
    return _settingDic;
}

@end
